﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Sales
{	public partial class OpenCash : Form
	{	private ToolStripMenuItem menuItem = null;

		public OpenCash() : this(null)
		{
		}

		public OpenCash(ToolStripMenuItem menuItem)
		{	InitializeComponent();
			this.menuItem = menuItem;
			if(menuItem != null)
				menuItem.Tag = this;
		}

		private void OpenCash_FormClosed(object sender, FormClosedEventArgs e)
		{	if(menuItem != null)
				menuItem.Tag = null;
		}

		private void OpenCash_Load(object sender, EventArgs e)
		{	cashRegisterOpenTableAdapter.Fill(sales.CashRegisterOpen, Util.CashRecorder.SerialNumber);
		}

		private DataGridViewCell clickedCell = null;
		private void closedCashRegisters_MouseDown(object sender, MouseEventArgs e)
		{	clickedCell = null;
			if(e.Button == MouseButtons.Right)
			{	DataGridView.HitTestInfo hit = closedCashRegisters.HitTest(e.X, e.Y);
				if(hit.Type == DataGridViewHitTestType.Cell)
					closedCashRegisters.CurrentCell = clickedCell = closedCashRegisters.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
			}
		}

		private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
		{	openToolStripMenuItem.Enabled = clickedCell != null;
		}

		private void openToolStripMenuItem_Click(object sender, EventArgs e)
		{	
            object name, kkmNumber;
			if(MessageBox.Show
			(	this
			,	string.Format
				(	Properties.Resources.strCashOpen
				,	name		= clickedCell.OwningRow.Cells[nameDataGridViewTextBoxColumn.Index		].Value
				,	kkmNumber	= clickedCell.OwningRow.Cells[kkmNumberDataGridViewTextBoxColumn.Index	].Value
				)
			,	Properties.Resources.strConfirmation
			,	MessageBoxButtons.YesNo
			,	MessageBoxIcon.Question
			,	MessageBoxDefaultButton.Button1
			) == DialogResult.Yes)
			{	DIProject.DataSets.Sales.CashRegisterOpenRow row = (DIProject.DataSets.Sales.CashRegisterOpenRow)((DataRowView)clickedCell.OwningRow.DataBoundItem).Row;
				System.Nullable<int> CashRegisterId	= row.id;
                DataSets.UtilTableAdapters.UtilQueries userIdAdapter = new DIProject.DataSets.UtilTableAdapters.UtilQueries();
                object obj = userIdAdapter.UserIdFromLogin(FirstForm.Login);
				System.Nullable<int> OpenUserId = null;
                if (!Convert.IsDBNull(obj))
                    OpenUserId = (Int32)obj;
				string Note							= null;
				System.Nullable<System.DateTime> MD	= DateTime.Now;
				string MW							= FirstForm.Login;
				try
				{	cashRegisterOpenTableAdapter.Update(CashRegisterId, OpenUserId, Note, MD, MW);
					row.Delete();
				// Открытие сессии на фискальном регистраторе
					Util.CashRecorder.OpenSession();
					MessageBox.Show
					(	this
					,	string.Format(Properties.Resources.strCashOpened, name, kkmNumber)
					,	Properties.Resources.strConfirmation
					,	MessageBoxButtons.OK
					,	MessageBoxIcon.Information
					);
				}
				catch(Exception ex)
				{	MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
		{	cashRegisterOpenTableAdapter.Fill(sales.CashRegisterOpen, Util.CashRecorder.SerialNumber);
		}
	}
}